package com.atguigu.mall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atguigu.common.page.PageFactory;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.mall.product.dao.SkuInfoDao;
import com.atguigu.mall.product.entity.SkuInfoEntity;
import com.atguigu.mall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SkuInfoEntity> page = PageFactory.defaultPage();
        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        String key  = (String) params.get("key");
        Boolean isKey = StrUtil.isNotBlank(key);
        wrapper.like(isKey, SkuInfoEntity::getSkuName,key)
                .or().eq(isKey,SkuInfoEntity::getSkuId,key);
        String catelogIdStr  = (String) params.get("catelogId");
        if (StrUtil.isNotBlank(catelogIdStr)&&!catelogIdStr.equals("0")){
            Long catelogId = Long.parseLong(catelogIdStr);
            wrapper.eq(SkuInfoEntity::getCatalogId,catelogId);
        }
        String brandIdStr  = (String) params.get("brandId");
        if (StrUtil.isNotBlank(brandIdStr) && !brandIdStr.equals("0")){
            Long brandId = Long.parseLong(brandIdStr);
            wrapper.eq(SkuInfoEntity::getBrandId,brandId);
        }
        String min  = (String) params.get("min");
        if (StrUtil.isNotBlank(min)){
            wrapper.ge(SkuInfoEntity::getPrice,new BigDecimal(min));
        }
        String max  = (String) params.get("max");
        if (StrUtil.isNotBlank(max) && !max.equals("0")){
            wrapper.le(SkuInfoEntity::getPrice,new BigDecimal(max));
        }
        this.baseMapper.selectPage(page,wrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public List<SkuInfoEntity> getSkuBySkuId(Long spuId) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuInfoEntity::getSpuId,spuId);
        List<SkuInfoEntity> list = this.list(wrapper);
        return list;
    }

}